package cn.gov.customs.h2018.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.google.common.collect.Lists;
import cn.gov.customs.h2018.pojo.Article;
import cn.gov.customs.h2018.service.ParallelQuery;

@Service("Lzhp2QueryImpl")
public class Lzhp2QueryImpl implements ParallelQuery {

  private final String url = "http://jsonplaceholder.typicode.com/posts/";
  @Autowired RestTemplate rest;

  @Override
  public List<Article> query(int articleCount) {
    List<Article> result = Lists.newArrayList();

    List<Integer> idList = Lists.newArrayList();
    for (int i = 0; i < articleCount; i++) {
      idList.add(i);
    }

    idList
        .stream()
        .parallel()
        .forEach(
            id -> {
              ResponseEntity<Article> temp = rest.getForEntity(url + (id + 1), Article.class);
              result.add(temp.getBody());
            });

    return result;
  }
}
